<?php
	class ApplicationManager {

		var $cache_arr = '../temp/navi.cache';

		function __construct($db) {
			$this->db = $db;
		}

		# 캐시가 존재한다면 삭제한다.
		function _refresh_cache() {
			//echo "renamed cache>>";
			if(is_file($this->cache_arr)==true){
				@file_put_contents($this->cache_arr, NULL);
				return;
				//@rename($this->cache_arr, sprintf('./temp/_remove/navi%s.cache', date('ymdhis')));
				//system("del /Q/F C:\\_apm\\htdocs\\ggoggo_framework\\temp\\navi.cache");
			}
			return;
		}

		# 파일을 검색하여 패키지 테이블에 업데이트
		function update_packages() {
			$this->_refresh_cache();

			$path = @opendir('../applications/');
			$ret = array();
			while ($list = @readdir($path)) {
				# ?????.????.????.php 와 같은 파일을 만을 arraydir에 추가한다.
				if (substr($list, -4) == '.php' && substr_count($list, ".") == 3 && substr($list,0,6) != "_ntdd_") {
					$arraydir[] = $list;
				}
			}
			//Nlog::puts('$arraydir', $arraydir);
			@closedir($path);
			# 일치하는 파일만을 정규식으로 추출한다.
			foreach($arraydir as $key=>$val){
				preg_match("/\.(.*?)\./i", $val, $matches);
				$match[] = $matches[1];
			}
			sort($match);
			$arr = array_unique($match);
			//Nlog::xmp($arr);

			# 패키지키 테이블에서 검사후 없으면 삽입
			foreach($arr as $key=>$val){
				//Nlog::xmp($val);
				$packselect = $this->db->get($que=sprintf("SELECT * FROM `ggo_cfg_packages_pref` WHERE `packages_key` = '%s'\n", $val));
				//Nlog::xmp($que);
				//Nlog::xmp($packselect);
				if(!$packselect['has']) {
					$issucc = $this->db->que($que=sprintf("INSERT INTO `ggo_cfg_packages_pref` SET `packages_key` = '%s';\n", $val));
					if($issucc['affected'] == 1){
						$ret[] = sprintf("패키지키 %s를 새롭게 업데이트 하였습니다.\n",$val);
					}
				}
			}

			# 파일을 패키지에서 삭제 해서 존재하지 않은 경우 패키지 키를 삭제하여 업데이트.
			$retcff = $this->db->query($que=sprintf("SELECT `packages_key` FROM `ggo_cfg_packages_pref`;"));
			$cffarr = array();
			$delarr = array();
			while($row = $retcff->fetch_array(MYSQLI_ASSOC)){
				$cffarr[] = $row['packages_key'];
			}
			//Nlog::puts($cffarr);

			foreach($cffarr as $key=>$val){
				$retdel = $this->db->get($que=sprintf("SELECT * FROM `ggo_cfg_packages` WHERE `package_key`= '%s'", $val));
				//Nlog::puts($retdel);
				if((int)$retdel['cnt'] <= 0) {
					$delarr[] = $val;
				}
			}
			//Nlog::puts($delarr);
			foreach($delarr as $key=>$val){
				$retdel = $this->db->que($que=sprintf("DELETE FROM `ggo_cfg_packages_pref` WHERE `packages_key`= '%s'", $val));
				if($retdel['affected'] == 1){
					$ret[] = sprintf("%s 패키지키는 패키지 파일이 존재 하지 않아 삭제 되었습니다.\n", $val);
				}
			}

			# 패키지 파일을 새롭게 업데이트한다.
			foreach($arr as $key=>$val){
				//echo $val;
				foreach($arraydir as $k=>$v){
					if(strstr($v, $val)){
						$narr[$val][] = $v;
					}
				}
			}
			//Nlog::xmp($narr);
			foreach($narr as $key=>$val){
				foreach($val as $k=>$v){
					$retselect = $this->db->get($que=sprintf("SELECT * FROM `ggo_cfg_packages` WHERE `package_key` = '%s' AND `package_file` = '%s';\n", $key, $v));
					if(!$retselect['has']) {
						$issucc = $this->db->que($que=sprintf("INSERT INTO `ggo_cfg_packages` SET `package_key` = '%s', `package_file` = '%s';\n", $key, $v));
						if($issucc['affected'] == 1){
							$ret[] = sprintf("%s 패키지의 %s 파일을 새롭게 업데이트 하였습니다.\n",$key,$v);
						}
					}
					//Nlog::xmp($retselect);
				}
			}
			//Nlog::xmp($ret);
			return $ret;
		}

		# 패키지를 비움
		function empty_packages() {
			$this->_refresh_cache();
			$issucc1 = $this->db->que($que=sprintf("TRUNCATE TABLE `ggo_cfg_packages`;"));
			$issucc2 = $this->db->que($que=sprintf("ALTER TABLE `ggo_cfg_packages` AUTO_INCREMENT = 1;"));
			//Nlog::xmp($issucc1);
			//Nlog::xmp($issucc2);
		}

		# 패키지 목록을 가져옴
		function get_packages($arg1, $arg2) {
			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_packages` ORDER BY `puid` DESC LIMIT %s, %s;", $arg1, $arg2));
			return $ret;
		}

		# 패키지 키에 따른 목록을 가져옴.
		function get_packages_by_key($sortkey) {
			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_packages` WHERE `package_key` = '%s';", $sortkey));
			return $ret;
		}

		# 패키지키 목록을 가져옴
		function get_packages_key($arg1, $arg2) {
			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_packages_pref` ORDER BY `puid` DESC LIMIT %s, %s;", $arg1, $arg2));
			return $ret;
		}

		# 패키지 정보를 수정.
		function modify_packages($pmap) {
			$this->_refresh_cache();
			$cnt = 0;
			foreach($pmap as $key=>$val){
				$ret = $this->db->que($que=sprintf("UPDATE `ggo_cfg_packages` SET `package_filename` = '%s', `package_desc` = '%s' WHERE `puid` = %s;", $val['package_filename'], $val['package_desc'], $val['puid']));
				if($ret['has']){
					$cnt += (int)$ret['affected'];
				}
				//Nlog::put('que',$que);
			}
			//Nlog::put('lastquery',$db->get_latest_query());
			//Nlog::put('cnt', $cnt);
			return $cnt;
		}

		# 패키지키 정보를 수정.
		function modify_packages_key($pmap) {
			$this->_refresh_cache();
			$cnt = 0;
			foreach($pmap as $key=>$val){
				$ret = $this->db->que($que=sprintf("UPDATE `ggo_cfg_packages_pref` SET `packages_name` = '%s', `packages_init_file` = '%s', `param` = '%s' WHERE `puid` = %s;", $val['packages_name'], $val['packages_init_file'], $val['param'], $val['puid']));
				if($ret['has']){
					$cnt += (int)$ret['affected'];
				}
				//Nlog::put('ret',$ret);
			}
			//Nlog::put('lastquery',$this->db->get_latest_query());
			//Nlog::put('cnt',$cnt);
			return $cnt;
		}

		# 패키지를 생성.
		function create_packages($pmap) {
			$this->_refresh_cache();
			$fname = $pmap['package_filename'];
			$pfname = implode('.', $fname).".php";

			//Nlog::xmp($pfname);
			if($fname[0] == 'admin'){
				if($fname[2] == 'process'){
					$template = IoFileAndDir::nfilereader('../labs/admin.process.template.txt');
				} else {
					$template = IoFileAndDir::nfilereader('../labs/admin.manager.template.txt');
				}
			}
			$ret = IoFileAndDir::nfilewriter('../applications/'.$pfname, $template, FALSE);
			//Nlog::xmp($template);
			if($ret){
				$cnt = 1;
				$updated_packages = $this->update_packages();
			} else {
				$cnt = 0;
			}

			return $cnt;
		}

		# 패키지를 삭제
		function delete_packages($pmap){
			$this->_refresh_cache();
			$cnt = 0;
			foreach($pmap as $key=>$val){
				//Nlog::put('val', $val);
				$ret = $this->db->get($que=sprintf("SELECT * FROM `ggo_cfg_packages` WHERE `puid` = %s;", $val['puid']));
				//Nlog::put('que', $que);
				//Nlog::put('ret', $ret);
				if($ret['has']){
					@rename($ret['res']['package_file'], sprintf('../temp/_ntdd_%s', $ret['res']['package_file']));
					$retd = $this->db->que($que = sprintf("DELETE FROM `ggo_cfg_packages` WHERE `puid` = %s;", $val['puid']));
					if($retd['has']){
						$cnt += (int)$retd['affected'];
					}
				}
			}
			//Nlog::put('lastquery',$db->get_latest_query());
			//Nlog::put('cnt', $cnt);

			return $cnt;
		}

		# 모듈의 목록을 가져옴
		function get_modules($arg1, $arg2) {
			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_modules_pref` ORDER BY muid DESC LIMIT %s, %s;", $arg1, $arg2));
			//Nlog::put('ret',$ret);
			if( $ret['res'] ){
				foreach($ret['res'] as $key=>$val){
					//Nlog::put('val[muid]',$val['muid']);
					$ret['res'][$key]['packages'] = $this->get_module_package($val['muid']);
				}
				//Nlog::put('ret',$ret);
				return $ret;
			}
		}

		# 모듈과 연결된 패키지 목록을 가져옴
		function get_module_package($muid) {
			$ret = $this->db->q($que=sprintf("SELECT `cm`.*, (SELECT `packages_name` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `package_name`, (SELECT `packages_key` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `package_key` FROM `ggo_cfg_modules` `cm` WHERE `muid`='%d'", $muid));
			if(count($ret) > 0){
				//Nlog::put('ret',$ret);
				return $ret;
			}
		}

		# 모듈의 정보를 수정
		function modify_modules($pmap) {
			$this->_refresh_cache();
			$cnt = 0;
			foreach($pmap as $key=>$val){
				$ret = $this->db->que($que=sprintf("UPDATE `ggo_cfg_modules_pref` SET `modules_name` = '%s', `modules_init_package` = '%s' WHERE `muid` = %s;", $val['modules_name'], $val['modules_init_package'], $val['muid']));
				if($ret['has']){
					$cnt += (int)$ret['affected'];
				}
				//Nlog::put('ret',$ret);
			}
			//Nlog::put('lastquery',$this->db->get_latest_query());
			//Nlog::put('cnt',$cnt);
			return $cnt;
		}

		# 모듈을 생성
		function create_modules($pmap) {
			$this->_refresh_cache();
			//$ret = $this->db->que($que = sprintf("SELECT * "))
			$ret1 = $this->db->que($que = sprintf("INSERT INTO `ggo_cfg_modules_pref` SET `modules_name` = '%s', `modules_init_package` = '%s';\n", $pmap['modules_name'], $pmap['modules_init_pacakge']));
			//Nlog::xmp($ret1);
			if($ret1['has']){
				$ret2 = $this->db->que($que = sprintf("INSERT INTO `ggo_cfg_modules` SET `muid` = (SELECT muid FROM `ggo_cfg_modules_pref` ORDER BY muid DESC LIMIT 1), `puid`='%s';\n", $pmap['modules_init_pacakge']));
				//Nlog::xmp($ret2);
				if($ret2['has']){
					$cnt = 1;
				} else {
					$cnt = 0;
				}
			} else {
				$cnt = 0;
			}

			return $cnt;
			//$ret = $this->db->que($que = sprintf("INSERT INTO `ggo_cfg_modules_pref` SET `modules_name` = '%s', `modules_init_package` = (SELECT packages_key FROM ggo_cfg_packages_pref WHERE `puid`='%s');\n", $pmap['modules_name'], $pmap['modules_init_pacakge']));
		}

		# 키값을 기준으로 모듈을 이름을 리턴
		function get_module_name($muid) {
			$ret = $this->db->getone($que=sprintf("SELECT `modules_name` FROM `ggo_cfg_modules_pref` WHERE `muid`=%d", $muid));
			//Nlog::xmp($que);
			//Nlog::xmp($ret);
			if($ret['has']){
				$retval = $ret['modules_name'];
			}
			if(count($ret) > 0){
				//Nlog::put('ret',$ret);
				return $retval;
			}
		}

		# 모듈을 삭제
		function delete_modules($pmap){
			$this->_refresh_cache();
			//Nlog::xmp($pmap);
			foreach($pmap as $key=>$val){
				$cnt = 0;
				$ret1 = $this->db->que($que1=sprintf("DELETE FROM `ggo_cfg_modules_pref` WHERE `muid` = %s;", $val['muid']));
				if($ret1['has']){
					$ret2 = $this->db->que($que2=sprintf("DELETE FROM `ggo_cfg_modules` WHERE `muid` = %s;", $val['muid']));
					if($ret2['has']){
						$cnt += (int)$ret1['affected'];
					}
				}
			}
			//Nlog::put('lastquery',$db->get_latest_query());
			//Nlog::put('cnt', $cnt);

			return $cnt;
		}

		# 모듈에 패키지를 연결
		function linkage_packages($pmuid, $pmap) {
			$this->_refresh_cache();
			$gmp = $this->get_module_package($pmuid);
			//Nlog::xmp($gmp);

			foreach($gmp as $key=>$val){
				$gmpt[] = $val['puid'];
			}
			//Nlog::put('gmpt 연결되어있던 패키지의 배열', $gmpt);

			//Nlog::put('linkarr1 새롭게 링크될 배열', $linkarr1);
			$linkarr1 = array_intersect($pmap, $gmpt);
			$cnt1 = count($linkarr1);

			//Nlog::put('linkarr2 변경사항이 없는 배열',$linkarr2);
			$linkarr2 = array_diff($pmap, $gmpt);
			$cnt2 = 0;
			if(count($linkarr2) > 0) foreach($linkarr2 as $key=>$val){
				$que2 = sprintf("INSERT INTO `ggo_cfg_modules` SET `muid` = '%s', `puid` = '%s';", $pmuid, $val);
				$ret2 = $this->db->que($que2);
				if($ret2['has']){
					$cnt2 += (int)$ret2['affected'];
				}
			}

			//Nlog::put('linkarr3 삭제될 배열',$linkarr3);
			$linkarr3 = array_diff($gmpt, $pmap);
			$cnt3 = 0;
			if(count($linkarr3) > 0) foreach($linkarr3 as $key=>$val){
				$que3 = sprintf("DELETE FROM `ggo_cfg_modules` WHERE `muid` = '%s' AND `puid` = '%s';", $pmuid, $val);
				$ret3 = $this->db->que($que3);
				if($ret3['has']){
					$cnt3 += (int)$ret3['affected'];
				}
			}

			$retarr = array(
				'cnt1' => $cnt1,
				'cnt2' => $cnt2,
				'cnt3' => $cnt3
			);

			//Nlog::put('전체 카운팅', $retarr);
			return $retarr;
		}

		# 카테고리의 목록을 가져옴
		function get_categories($arg1, $arg2) {
			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_categories_pref` ORDER BY cuid DESC LIMIT %s, %s;", $arg1, $arg2));
			//Nlog::put('ret',$ret);
			if( $ret['has'] ){
				foreach($ret['res'] as $key=>$val){
					//Nlog::put('val[muid]',$val['muid']);
					$linkage_modules = $this->get_category_module($val['cuid']);
					//foreach($linkage_modules as $key=>$val){
						//echo $val['muid'];
						//$ret2 = $this->db->getone($que2=sprintf("SELECT `packages_init_file` FROM `ggo_cfg_packages_pref` WHERE `puid` = %d", $val['muid']));
						//Nlog::xmp($ret2);
						//if($ret2['has']){
							//$ret['res'][$key]['modules'] = ;
						//	$linkage_modules[$key]['packages_init_file'] = $ret2['packages_init_file'];
						//}
					//}
					$ret['res'][$key]['modules'] = $linkage_modules;
					//Nlog::put('linkage_modules', $linkage_modules);

					//module_package($val['muid']);
				}
				//Nlog::put('ret',$ret);


				return $ret;
			}
		}

		# 카테고리와 연결된 모듈 목록을 가져옴
		function get_category_module($muid) {
			$ret = $this->db->q($que=sprintf("SELECT `cc`.*, (SELECT `modules_name` FROM `ggo_cfg_modules_pref` `cmp` WHERE `cc`.`muid` = `cmp`.`muid`) AS `modules_name` FROM `ggo_cfg_categories` `cc` WHERE `cuid`='%d'", $muid));
			if(count($ret) > 0){
				//Nlog::put('ret',$ret);
				return $ret;
			}
		}

		# 키값을 기준으로 카테고리 이름을 리턴
		function get_category_name($cuid) {
			$ret = $this->db->getone($que=sprintf("SELECT `category_name` FROM `ggo_cfg_categories_pref` WHERE `cuid`=%d", $cuid));
			//Nlog::xmp($que);
			//Nlog::xmp($ret);
			if($ret['has']){
				$retval = $ret['category_name'];
			}
			if(count($ret) > 0){
				//Nlog::put('ret',$ret);
				return $retval;
			}
		}

		# 카테고리의 정보를 수정
		function modify_categories($pmap) {
			$this->_refresh_cache();
			$cnt = 0;
			foreach($pmap as $key=>$val){
				$ret = $this->db->que($que=sprintf("UPDATE `ggo_cfg_categories_pref` SET `category_name` = '%s', `category_init_modules` = '%s' WHERE `cuid` = '%s';", $val['category_name'], $val['category_init_modules'], $val['cuid']));
				if($ret['has']){
					$cnt += (int)$ret['affected'];
				}
				//Nlog::put('ret',$ret);
			}
			//Nlog::put('lastquery',$this->db->get_latest_query());
			//Nlog::put('cnt',$cnt);
			return $cnt;
		}

		# 카테고리를 생성
		function create_categories($pmap) {
			$this->_refresh_cache();
			$ret1 = $this->db->que($que=sprintf("INSERT INTO `ggo_cfg_categories_pref` SET `category_name` = '%s', `category_init_modules` = '%s';", $pmap['category_name'], $pmap['category_init_modules']));
			//Nlog::xmp($ret1);

			if($ret1['has']){
				$ret2 = $this->db->que($que = sprintf("INSERT INTO `ggo_cfg_categories` SET `cuid` = (SELECT cuid FROM `ggo_cfg_categories_pref` ORDER BY cuid DESC LIMIT 1), `muid`='%s';\n", $pmap['category_init_modules']));
				//Nlog::xmp($que);
				if($ret2['has']){
					$cnt = 1;
				} else {
					$cnt = 0;
				}
			} else {
				$cnt = 0;
			}
			//Nlog::put('lastquery',$this->db->get_latest_query());
			return $cnt;
			//$ret = $this->db->que($que = sprintf("INSERT INTO `ggo_cfg_modules_pref` SET `modules_name` = '%s', `modules_init_package` = (SELECT packages_key FROM ggo_cfg_packages_pref WHERE `puid`='%s');\n", $pmap['modules_name'], $pmap['modules_init_pacakge']));
		}

		# 카테고리를 삭제
		function delete_categories($pmap) {
			$this->_refresh_cache();
			//Nlog::xmp($pmap);
			foreach($pmap as $key=>$val){
				$cnt = 0;
				$ret1 = $this->db->que($que1=sprintf("DELETE FROM `ggo_cfg_categories_pref` WHERE `cuid` = %s;", $val['cuid']));
				if($ret1['has']){
					$ret2 = $this->db->que($que2=sprintf("DELETE FROM `ggo_cfg_categories` WHERE `cuid` = %s;", $val['cuid']));
					if($ret2['has']){
						$cnt += (int)$ret1['affected'];
					}
				}
			}
			//Nlog::put('lastquery',$db->get_latest_query());
			//Nlog::put('cnt', $cnt);

			return $cnt;
		}

		# 카테고리에 모듈을 연결
		function linkage_modules($pcuid, $pmap) {
			$this->_refresh_cache();
			$gcp = $this->get_category_module($pcuid);
			//Nlog::xmp($gcp);

			foreach($gcp as $key=>$val){
				$gcpt[] = $val['muid'];
			}
			//Nlog::put('gcpt 연결되어있던 패키지의 배열', $gcpt);

			$linkarr1 = array_intersect($pmap, $gcpt);
			$cnt1 = count($linkarr1);
			//Nlog::put('linkarr1 새롭게 링크될 배열', $linkarr1);

			$linkarr2 = array_diff($pmap, $gcpt);
			$cnt2 = 0;
			if(count($linkarr2) > 0) foreach($linkarr2 as $key=>$val){
				$que2 = sprintf("INSERT INTO `ggo_cfg_categories` SET `cuid` = '%s', `muid` = '%s';", $pcuid, $val);
				$ret2 = $this->db->que($que2);
				if($ret2['has']){
					$cnt2 += (int)$ret2['affected'];
				}
			}
			//Nlog::put('linkarr2 변경사항이 없는 배열',$linkarr2);

			$linkarr3 = array_diff($gcpt, $pmap);
			$cnt3 = 0;
			if(count($linkarr3) > 0) foreach($linkarr3 as $key=>$val){
				$que3 = sprintf("DELETE FROM `ggo_cfg_categories` WHERE `cuid` = '%s' AND `muid` = '%s';", $pcuid, $val);
				$ret3 = $this->db->que($que3);
				if($ret3['has']){
					$cnt3 += (int)$ret3['affected'];
				}
			}
			//Nlog::put('linkarr3 삭제될 배열',$linkarr3);

			$retarr = array(
				'cnt1' => $cnt1,
				'cnt2' => $cnt2,
				'cnt3' => $cnt3
			);

			//Nlog::put('전체 카운팅', $retarr);
			return $retarr;
		}

		# 전체 네비게이션 리스트를 가져옴.
		function navi_tree() {

			if(is_file($this->cache_arr)==true){
				//echo "used cache>>";
				//Nlog::xmp($fgc);
				$fs = IoFileAndDir::get_size($this->cache_arr);
				if($fs > 0){
					//echo "size 0";
					$fgc = file_get_contents($this->cache_arr);
					$ret = unserialize($fgc);
					return $ret;
				}
			}

			//exit();

			$ret = $this->db->gets($que=sprintf("SELECT * FROM `ggo_cfg_categories_pref` ORDER BY cuid ASC"));
			if( $ret['has'] ){
				foreach($ret['res'] as $key=>$val){
					$ret2 = $this->db->q($que=sprintf("SELECT `cc`.*, (SELECT `modules_name` FROM `ggo_cfg_modules_pref` `cmp` WHERE `cc`.`muid` = `cmp`.`muid`) AS `modules_name`, (SELECT `modules_init_package` FROM `ggo_cfg_modules_pref` `cmp` WHERE `cc`.`muid` = `cmp`.`muid`) AS `modules_init_package` FROM `ggo_cfg_categories` `cc` WHERE `cuid`='%d'", $val['cuid']));
					if(count($ret2) > 0){
						$ret['res'][$key]['modules'] = $ret2;
						foreach($ret2 as $key2=>$val2){
							$ret3 = $this->db->q($que3=sprintf("SELECT `cm`.*, (SELECT `packages_name` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `package_name`, (SELECT `packages_key` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `package_key`, (SELECT `packages_init_file` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `packages_init_file`, (SELECT `param` FROM `ggo_cfg_packages_pref` `cpp` WHERE `cm`.`puid` = `cpp`.`puid`) AS `param`  FROM `ggo_cfg_modules` `cm` WHERE `muid`='%d' ", $val2['muid']));
							if(count($ret3) > 0){
								$ret['res'][$key]['modules'][$key2]['packages'] = $ret3;
								foreach($ret3 as $key3=>$val3){
									$ret4 = $this->db->gets($que4=sprintf("SELECT * FROM `ggo_cfg_packages` WHERE `package_key` = '%s';", $val3['package_key']));
									foreach($ret4['res'] as $key4=>$val4){
										$ret['res'][$key]['package_files'][] = $val4['package_file'];
										$ret['res'][$key]['modules'][$key2]['packages'][$key3]['packages_files'][] = $val4['package_file'];
									}
								}
							}
						}
					}
				}

				foreach($ret['res'] as $key=>$val){
					foreach($val['modules'] as $key1=>$val1){
						if( $val['category_init_modules'] == $val1['muid']){
							foreach($val1['packages'] as $key2=>$val2){
								if( $val1['modules_init_package'] == $val2['puid'] ){
									$init = $val2['packages_init_file'].$val2['param'];
								}
							}

						}
					}
					# 패키지를 클릭하였을때 init를 실행
					$ret['res'][$key]['init'] = $init;
				}

				@file_put_contents($this->cache_arr, serialize($ret));

				return $ret;
			}
		}
	}
?>